package com.zzq.consumer.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zzq.consumer.bean.Student;
import com.zzq.consumer.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zzq
 * @Date 2020/2/26 23:18
 * @Version 1.0
 */
@RestController
@RequestMapping("/consumer/students")
public class ConsumerController {

    @Autowired
    private StudentService service;

    @PostMapping("/save")
    public boolean save(Student student) {
        return service.saveStudent(student);
    }

    @DeleteMapping("/remove/{id}")
    public boolean remove(@PathVariable Integer id) {
        return service.deleteStudent(id);
    }

    @PutMapping("/update")
    public boolean update(Student student) {
        return service.updateStudent(student);
    }

    @GetMapping("/get/{id}")
    public Student getOne(@PathVariable Integer id) {
        return service.getStudent(id);
    }

    @GetMapping("/list")
    public List<Student> list() {
        return service.findAll();
    }

    @HystrixCommand(fallbackMethod = "getOneFallBackMethod3")
    @GetMapping("/test/{id}")
    public Student test(@PathVariable Integer id) {
        throw new RuntimeException("no found");
    }

    public Student getOneFallBackMethod3(Integer id) {
        Student student = new Student();
        student.setId(id);
        student.setName("zzq3");
        student.setAge(3);
        return student;
    }
}
